 $().ready(function() {
	//$('input[value="Upload"]').remove(); 
	//$('input[value="Remove"]').remove();
//function create_errormsg_container - To hold error messages in one palce
	$.fn.create_errormsg_container = function(err_element_id, form_id) {
		
    var addNewSpan = '<span id="'+err_element_id+'"></span>';
        $("#"+form_id).before(addNewSpan);
}



//function valuenotequals - Not allow particular value
$.validator.addMethod("valuenotequals", function(value, element, arg){
												 
  return arg != value;
 }, "Value must not equal arg.");

//function multi_dist_select - Checking Multiple Districts Values
$.validator.addMethod("multi_dist_select", function(value, element, arg){
						//alert(value.length);
						 if(value.length > 1 && ((value[0] == 0) || (value[0] == '_'))){
							return false;
						}
						else{
							return true;
						}

 }, "Please Select either All or Particular Districts");

//function chkbadchar - To filter some characters			
$.validator.addMethod("chkbadchar", function(value, element) {    
    	badch = new Array("insert", "select", "update", "delete", "drop", "alter", "change", "modify");
		   
    for (var k = 0; k < badch.length; k++) {
        if (value.toLowerCase().indexOf(badch[k]) != -1) {
            
            return false;
        }
    }
    return true;
    },'<p style="font-size:10px">Characters Not Allowed : insert, select, update, delete, drop, alter, change, modify.</p>');
 

 //spl char
 $.validator.addMethod("splchar", function(value, element) {    
    	badch = new Array("~","`","!","@","#","$","%","^","&","*","+","=","|",";","<",">","?","{","}","'","--","\"");
		   
    for (var k = 0; k < badch.length; k++) {
        if (value.toLowerCase().indexOf(badch[k]) != -1) {
            
            return false;
        }
    }
    return true;
    },'<p style="font-size:10px">Invalid characters </p>' );
 

 
 //spl and bad char for password
 $.validator.addMethod("checkpwd", function(value, element) {    
    	badch = new Array("#","$","&",";",":","<",">","\'","\"","insert", "select", "update", "delete", "drop", "alter", "change", "modify");
		   
    for (var k = 0; k < badch.length; k++) {
        if (value.toLowerCase().indexOf(badch[k]) != -1) {
            
            return false;
        }
    }
    return true;
    },'<p style="font-size:10px">Invalid bad and specialcharacters.</p>' );
 
 //validation for password
 $.validator.addMethod("validpassword", function(value, element) {
    return this.optional(element) ||
        /^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d])(?=.*[\W_]).*$/.test(value);
	}, "The password must contain a minimum of one lower case character," +
           " one upper case character, one digit and one special character..");
 
 //mobile number
 $.validator.addMethod("startmobilecode", function(value, element) {    
    	var mbnum = value;
		if ((mbnum.substring(0,1)=="9") || (mbnum.substring(0,1)=="0") || (mbnum.substring(0,1)=="8") || (mbnum.substring(0,1)=="7"))
		{
   			return true;
        }
    	else {
    		return false;
		}
    },'<p style="font-size:10px">Mobile Number should start with 0 or 9 or 8 or 7.</p>' );
 
//function chkfile_format_size - Restricting uploading file format and size - sivaguru
$.validator.addMethod("chkfile_format_size", function (val, element) {
    var ext = $(element).val().split('.').pop().toLowerCase();
    var allow = new Array('pdf');
   
	if($(element).val() != ''){
		
	
    if (jQuery.inArray(ext, allow) == -1 || element.files[0].size > 1048576) {
        return false
    } else {
        return true
    }
	
	}else {
        return true
    }
	
}, "Invalid file");


//function dateformat - Checking Date Format
$.validator.addMethod("dateformat",function(value, element) {
											
  if(value!= ''){
    dateStr = value;

    var slash1 = dateStr.indexOf("/");
	
   /* if (slash1 == -1) {
        slash1 = dateStr.indexOf("-");
		
    }*/

    // if no slashes or dashes, invalid date
    if (slash1 == -1) {
        //alert ("Enter a valid date in DD/MM/YYYY format"); 
        return 0;
        //return false; 
    }

    var dateDay = dateStr.substring(0, slash1)
    var dateMonthAndYear = dateStr.substring(slash1 + 1, dateStr.length);

    var slash2 = dateMonthAndYear.indexOf("/");
    /*if (slash2 == -1) {
        slash2 = dateMonthAndYear.indexOf("-");
    }*/

    // if not a second slash or dash, invalid date
    if (slash2 == -1) {
        //alert ("Enter a valid date in DD-MM-YYYY format"); 
        return 0;
    }

    var dateMonth = dateMonthAndYear.substring(0, slash2);
    var dateYear = dateMonthAndYear.substring(slash2 + 1, dateMonthAndYear.length);

    if ((dateMonth == "") || (dateDay == "") || (dateYear == "")) {
        //alert ("Enter a valid date in DD-MM-YYYY format"); 
        return 0;
    }

    // if any non-digits in the month, invalid date
    for (var x = 0; x < dateMonth.length; x++) {
        var digit = dateMonth.substring(x, x + 1);
        if ((digit < "0") || (digit > "9")) {
            //alert ("Enter a valid date in DD-MM-YYYY format, Invalid Month"); 
             return 0;
        }
    }

    // convert the text month to a number
    var numMonth = 0;
    for (var x = 0; x < dateMonth.length; x++) {
        digit = dateMonth.substring(x, x + 1);
        numMonth *= 10;
        numMonth += parseInt(digit);
    }

    if ((numMonth <= 0) || (numMonth > 12)) {
        //alert ("Enter a valid date in DD-MM-YYYY format, Invalid Month "+numMonth); 
        return 0;
    }

    // if any non-digits in the day, invalid date
    for (var x = 0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x + 1);
        if ((digit < "0") || (digit > "9")) {
            //alert ("Enter a valid date in DD-MM-YYYY format"); 
              return 0;
        }
    }

    // convert the text day to a number
    var numDay = 0;
    for (var x = 0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x + 1);
        numDay *= 10;
        numDay += parseInt(digit);
    }

    if ((numDay <= 0) || (numDay > 31)) {
        //alert ("Enter a valid date in DD-MM-YYYY format, Invalid Day"); 
          return 0;
    }

    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) {
        //alert ("Enter a valid date in DD-MM-YYYY format, Invalid Day"); 
          return 0;
    }

    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) {
        if (numDay > 30) {
            //alert ("Enter a valid date in DD-MM-YYYY format, Invalid Day"); 
              return 0;
        }
    }

    // if any non-digits in the year, invalid date
    for (var x = 0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x + 1);
        if ((digit < "0") || (digit > "9")) {
            //alert ("Enter a valid date in DD-MM-YYYY format, Invalid Year"); 
              return 0;
        }
    }

    // convert the text year to a number
    var numYear = 0;
    for (var x = 0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x + 1);
        numYear *= 10;
        numYear += parseInt(digit);
    }

    // Year must be a 2-digit year or a 4-digit year
    if ((dateYear.length != 2) && (dateYear.length != 4)) {
        //alert ("Enter a valid date in DD-MM-YYYY format, Invalid Year"); 
          return 0;
    }

    // if 2-digit year, use 50 as a pivot date
    if ((numYear < 50) && (dateYear.length == 2)) {
        numYear += 2000;
    }

    if ((numYear < 100) && (dateYear.length == 2)) {
        numYear += 1900;
    }

    if ((numYear <= 0) || (numYear > 9999)) {
        //alert ("Enter a valid date in DD-MM-YYYY format, Invalid Year"); 
          return 0;
    }

    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;

        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) {
            //alert ("Enter a valid date in DD-MM-YYYY format, Invalid Date"); 
              return 0;
        }

        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) {
            //alert ("Enter a valid date in DD-MM-YYYY format, Invalid Date"); 
              return 0;
        }
      }
    }
    // date is valid
      return 1;

        },"Please enter date in the format dd/mm/yyyy"
    );

//function date_notgreatertdy - Checking Date not greater than today
$.validator.addMethod("date_notgreater_tdy",function(value, element) {
            
            var d = new Date();
            var curr_day = d.getDate();
            var curr_month = d.getMonth();
            var curr_year = d.getFullYear();
            var str2 = value;
            var v_dt = parseInt(str2.substring(0, 2), 10);
            var v_mon = parseInt(str2.substring(3, 5), 10);
		
            var v_yr = parseInt(str2.substring(6, 10), 10);
            var dt1 = new Date(curr_year, curr_month, curr_day);
			
            var dt2 = new Date(v_yr, v_mon-1 , v_dt);
		
            if (dt1 < dt2) 
			{
               return false;
            }
			else{
        return true;}
        },"Date Should Not Be Greater Than Current Date"
    );

//function date_notlesstdy - Checking Date not greater than today
$.validator.addMethod("date_notless_tdy",function(value, element,param) {
           // alert(param);
            var d = new Date();
            var curr_day = d.getDate();
            var curr_month = d.getMonth();
            var curr_year = d.getFullYear();
            var str2 = value;
            var v_dt = parseInt(str2.substring(0, 2), 10);
            var v_mon = parseInt(str2.substring(3, 5), 10);
		
            var v_yr = parseInt(str2.substring(6, 10), 10);
            var dt1 = new Date(curr_year, curr_month, curr_day);
			
            var dt2 = new Date(v_yr, v_mon-1 , v_dt);
		
            if (dt1 > dt2) 
			{
               return false;
            }
        return true;
        },"Date Should Not Be Less Than Current Date"
    );

//function date_greater_than - Checking Date not greater than compared date
$.validator.addMethod("date_greater_than",function(value, element,param) {
												   
            var compare_id_value = $(param['compar_id']).val();
			
			var chk_format = jQuery.validator.methods.dateformat.call(this, compare_id_value, element);
				
				
			if(compare_id_value.length !=0 && chk_format !=0){

			 var chk_day = parseInt(value.substring(0, 2), 10);
            var chk_mon = parseInt(value.substring(3, 5), 10);
		
            var chk_yr = parseInt(value.substring(6, 10), 10);
			var chk_compare_day = parseInt(compare_id_value.substring(0, 2), 10);
            var chk_compare_mon = parseInt(compare_id_value.substring(3, 5), 10);
		
            var chk_compare_yr = parseInt(compare_id_value.substring(6, 10), 10);
			 var chk_date = new Date(chk_yr, chk_mon, chk_day);
			
            var compare_date = new Date(chk_compare_yr, chk_compare_mon , chk_compare_day);
			if (chk_date > compare_date) {
								
               return false;
            }
			else{
				return true;
			}
		}
       
        },"Date Should Not Be Greater Than End Date"
    );


//function date_less_than - Checking Date not lesser than compared date
$.validator.addMethod("date_less_than",function(value, element,param) {
												   
            var compare_id_value = $(param['compar_id']).val();
			
			var chk_format = jQuery.validator.methods.dateformat.call(this, compare_id_value, element);
				
				
			if(compare_id_value.length !=0 && chk_format !=0){

			 var chk_day = parseInt(value.substring(0, 2), 10);
            var chk_mon = parseInt(value.substring(3, 5), 10);
		
            var chk_yr = parseInt(value.substring(6, 10), 10);
			var chk_compare_day = parseInt(compare_id_value.substring(0, 2), 10);
            var chk_compare_mon = parseInt(compare_id_value.substring(3, 5), 10);
		
            var chk_compare_yr = parseInt(compare_id_value.substring(6, 10), 10);
			 var chk_date = new Date(chk_yr, chk_mon, chk_day);
			
            var compare_date = new Date(chk_compare_yr, chk_compare_mon , chk_compare_day);
			if (chk_date < compare_date) {
								
               return false;
            }
			else{
				return true;
			}
		}
       
        },'date_less_than'
    );

//function docyear - Checking Date not lesser than compared date
$.validator.addMethod("docyear",function(value, element,param) { 
										 var getyears = value.split('-');
										 var dd = new Date();
										 //alert(dd);
           								 var cur_yr = dd.getFullYear();
										 
										if(getyears[1] == undefined)// checking the Format Split by - symbol, Second array will be undefine if fails
										return false;
										var yrdiff = getyears[1] - getyears[0];
										var inc_sec_yr_by_one=Number(getyears[1]) + Number(1);
										if(isNaN(getyears[0]) == true || getyears[0].length != 4 || getyears[0] < 1999) //checking first part year
										return false;
										if(isNaN(getyears[1]) == true || getyears[1].length != 4 || getyears[1] < 1999)//checking second part year
										return false;
										if(getyears[0] >= getyears[1] || (yrdiff != 1))
										return false;
										if(getyears[0] > cur_yr)
										return false;
										else
										return true;
          
       
        },'Year should be in the format 1999-2000.'
    );
 $.validator.addMethod("year_not_greater",function(value, element) {
           var d = new Date();
           var curr_year = d.getFullYear();
           var year =  value;
                       //if(year <= 1640){ return "Year should";}
                       if (year > curr_year) {
              return false;
           }
                       
       return true;
       },"Year Should Not Be Greater Than Current Year"
   );
 
 });
/*var dispError = function(method){
	alert(method);
    return method;
}

$.validator.addMethod("functionA", function(value, element) {
    
        return value;
   
}, "MSG A");
$.validator.addMethod("functionB", function(value, element) {
    alert(jQuery.validator.methods.functionA.call(this, value, element));
	return false;
}, "MSG B");
$.validator.addMethod("date_chk", function(value, element,params) {
   if(params['chk_type'] == 'dateformat'){
   }
   if(params['chk_type'] == 'date_notgreatertdy'){
   }
   if(params['chk_type'] == 'date_notlesstdy'){
   }
}, "MSG B");

*/